package com.xueyu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.RelativeDao;
import com.xueyu.dao.SituationDao;
import com.xueyu.dao.personinfoDao;
import com.xueyu.domain.Relative.RelativeBean;
import com.xueyu.domain.Situation.SituationBean;
import com.xueyu.domain.personinfo.personinfoBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/situation")
public class SituationContoller {

    @Autowired
    SituationDao situationDao;
    @Autowired
    personinfoDao personinfoDao;
    @Autowired
    RelativeDao relativeDao;

    @RequestMapping("/add")
    public ResultBean add(@RequestBody SituationBean situationBean) throws Exception{
        if(situationBean.getSituationId() == 0){
            situationDao.insert(situationBean);
        }else{
            situationDao.updateById(situationBean);
        }
        return new ResultBean();
    }

    @RequestMapping("/list")
    public ResultBean list(@RequestBody Map map){
        int curPage = (Integer) map.get("curPage");
        int size = (Integer) map.get("size");
        Page<SituationBean> pager = new Page<SituationBean>(curPage,size);
        List<SituationBean> situationList = situationDao.listPage(pager, map);
        pager.setRecords(situationList);
        List<personinfoBean> personinfoList = personinfoDao.selectList(null);
        List<RelativeBean> relativeList = relativeDao.selectList(null);
        ResultBean resultBean = new ResultBean();
        resultBean.put("pager",pager);
        resultBean.put("personinfoList",personinfoList);
        resultBean.put("relativeList",relativeList);
        return resultBean;
    }

    @RequestMapping("/del/{situationId}")
    public ResultBean del(@PathVariable("situationId") int situationId){
        situationDao.deleteById(situationId);
        return new ResultBean();
    }

    @RequestMapping("/getId/{situationId}")
    public ResultBean getId(@PathVariable("situationId") int situationId){
        ResultBean resultBean = new ResultBean();
        Map map = new HashMap();
        if(situationId>0){
            SituationBean situation = situationDao.selectById(situationId);
            map.put("situation",situation);
        }
        List<personinfoBean> personinfoList = personinfoDao.selectList(null);
        List<RelativeBean> relativeList = relativeDao.selectList(null);
        map.put("personinfoList",personinfoList);
        map.put("relativeList",relativeList);
        resultBean.put("data",map);
        return resultBean;
    }

}
